<?php
/**
Version 0.1
SEMF - Web Desiging Framework for Sport Event Managment
Copyright (C) 2006, SEMF Developers

This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.

This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
GNU General Public License for more details.

You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
*/

class links_parser
{
	private static $semf_root;
	private $script;
	private $semantic;
	private $namespace;
	
	public function __construct($semf_root, $semantic) 
	{
		if($semf_root == NULL)
		{
			$this->semf_root = "../../../";
		}
		else
		{
			$this->semf_root = $semf_root;
		}
		include_once( $semf_root."page_designer/script/script.php");
		include_once( $semf_root."page_designer/script/statement.php");
		
		$this->semantic = $semantic;
		$this->namespace = "SEMF.comp";
	}
	
	public function parse($properties)
	{
		$ids = array_keys($properties);
		foreach( $ids as $id)
		{
			$type = $properties[$id]['type'];
			$data_binding_properties = $properties[$id]['properties']['Links'];
			
			if( $data_binding_properties == NULL )
			{
				continue;
			}
			$this->parse_component($id, $type, $data_binding_properties);
		}
	}
	
	private function parse_component($id, $type, $properties)
	{
		//print_r($properties);
		$property_keys = array_keys($properties);
		$script = NULL;
		
		
					
		foreach($property_keys as $key)
		{
			//print( $properties[$key]);
			$describtion = "<h3>Links of ".$type." for ".$key."</h3>";
			$project_home = $this->semantic->get_project_home();
			$value = $properties[$key];
			
			$pos = strrpos($value, "=");
			if($pos !== false) //then get the later part..
			{
				$value = substr($value, $pos+1);
			}
			else
			{
				$pos = strpos($value, "."); //this is for the module name
				if($pos !== false)
				{
					$module_name = substr($value, 0, $pos);
					$link_name = substr($value, $pos+1);
					$value = $this->derive_link($module_name, $link_name);
				}
			}

			$script = new script($value, $this->semf_root, $project_home, $describtion);
			$parse_tree = $script->get_parse_tree();
			
			$tokens = $script->get_tokens();
			
			$this->semantic->evaluate_databinding_statement($parse_tree, $tokens,
						 $id, $key, $description);
		}
		
		$comp_class_name = ${type}."Component";
		$obj = new ${comp_class_name}();
		$link_info_list = $obj->get_link_info($id, $properties);
		
		$this->semantic->associate_property_list($link_info_list, $id);	
	}
	
	private function derive_link($module_name, $link_name)
	{
		
		return "\"../".$module_name."/".$link_name."_html.php\"";
	}
}
?>